package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.example.domain.ResponseResult;
import org.example.domain.dto.RoleListDto;
import org.example.domain.entity.Role;
import org.example.domain.vo.PageVo;
import org.example.domain.vo.RoleListVo;
import org.example.enums.AppHttpCodeEnum;
import org.example.exception.SystemException;
import org.example.mapper.RoleMapper;
import org.example.service.RoleService;
import org.example.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    private final RoleMapper roleMapper;

    @Override
    public List<String> selectRoleKeyByUserId(Integer id) {
        if(id == 1){
            List<String> roleKey = new ArrayList<>();
            roleKey.add("admin");
            return roleKey;
        }
        return roleMapper.selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult roleList(RoleListDto roleListDto) {

        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(roleListDto.getRoleName()),Role::getRoleName,roleListDto.getRoleName());
        if(roleListDto.getStatus() != null){
            queryWrapper.eq(Role::getStatus,roleListDto.getStatus());
        }

        Page<Role> page = new Page<>();
        page.setCurrent(roleListDto.getPageNum());
        page.setSize(roleListDto.getPageSize());
        page(page,queryWrapper);

        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public boolean changeRoleStatus(Role role) {
        return roleMapper.changeRoleStatus(role);
    }

    @Override
    public ResponseResult addRole(Role role) {
        List<Role> roles = roleMapper.selectRoleByRoleName(role);
        if(CollectionUtils.isNotEmpty(roles)){
            throw new SystemException(AppHttpCodeEnum.ROLE_EXIST);
        }
        return ResponseResult.okResult(roleMapper.addRole(role));
    }

    @Override
    public ResponseResult getAllRoles() {
        List<Role> roles = roleMapper.getAllRoles();
        List<RoleListVo> roleListVos = BeanCopyUtils.copyBeanList(roles, RoleListVo.class);
        return ResponseResult.okResult(roleListVos);
    }

    @Override
    public ResponseResult updateRole(Role role) {
        List<Role> roles = roleMapper.selectRoleByRoleName(role);
        if(CollectionUtils.isNotEmpty(roles)){
            throw new SystemException(AppHttpCodeEnum.ROLE_EXIST);
        }
        return ResponseResult.okResult(updateById(role));
    }
}
